package com.wuage.interfaces.account;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wuage.application.IUacAccountService;
import com.wuage.domain.model.UacAccount;
import com.wuage.infrastructure.utils.Response;
import com.wuage.interfaces.BaseController;
import com.wuage.interfaces.PagerVO;
import com.wuage.interfaces.account.domain.AccountQueryDO;
import com.wuage.interfaces.account.domain.AdminSaveOrUpdateAccountDO;
import com.wuage.interfaces.account.vo.AccountVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author Shynin
 * @version 1.0
 * @date 2020-09-07 00:46
 */
@Api(tags = "管理员-{用户}操作相关接口")
@RestController
@RequestMapping("/admin")
public class AdminAccountController extends BaseController {

    @Resource
    private IUacAccountService accountService;

    @PostMapping("/page/accounts")
    @ApiOperation("管理员分页获取用户列表")
    public Response<PagerVO<AccountVO>> getUserByPage(@RequestBody AccountQueryDO queryDO) {
        IPage<UacAccount> usersByPage = accountService.getUsersByPage(queryDO);
        PagerVO<AccountVO> pagerVO = PagerVO.assembleIPager2PagerVO(usersByPage, AccountVO::assembleAccounts2Vos);
        return Response.success(pagerVO);
    }

    @PostMapping("/account")
    @ApiOperation("新增用户")
    public Response<AccountVO> addUser(@RequestBody @Valid AdminSaveOrUpdateAccountDO adminAddAccountDO) {
        UacAccount uacAccount = accountService.saveOrUpdateAccount(adminAddAccountDO);
        return Response.success(AccountVO.assembleAccount2VO(uacAccount));
    }

    @PutMapping("/account")
    @ApiOperation("更新")
    public Response<AccountVO> updateUser(@RequestBody @Valid AdminSaveOrUpdateAccountDO adminAddAccountDO) {
        UacAccount uacAccount = accountService.saveOrUpdateAccount(adminAddAccountDO);
        return Response.success(AccountVO.assembleAccount2VO(uacAccount));
    }

    @PutMapping("/accounts/enable/{id}")
    @ApiOperation("启用/停用用户")
    public Response<Boolean> enableAccount(@PathVariable(value = "id", required = true) Long id) {
        Boolean isSucceed = accountService.enableAccount(id);
        return Response.success(isSucceed);
    }

    @DeleteMapping("/accounts/{id}")
    @ApiOperation("删除用户")
    public Response<Boolean> deleteAccount(@PathVariable(value = "id", required = true) Long id) {
        boolean result = accountService.removeById(id);
        return Response.success(result);
    }

    @PutMapping("/accounts/password/{id}")
    @ApiOperation("重置用户密码")
    public Response<String> resetPassword(@PathVariable(value = "id", required = true) Long id) {
        String password = accountService.resetPassword(id);
        return Response.success(password);
    }
}
